package cn.itsource.listener;


import cn.itsource.listener.event.GeTuiMessageEvent;
import cn.itsource.pojo.bo.GeTuiBo;
import cn.itsource.pojo.domain.PayOrder;
import com.alibaba.fastjson.JSON;
import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.GtApiConfiguration;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.api.UserApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.AudienceDTO;
import com.getui.push.v2.sdk.dto.req.CidAliasListDTO;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 📖：beishan
 * 🗓️：2024/4/21 10:11
 * =====================
 * 监听
 */
@Component
public class GeTuiMessageListener implements ApplicationListener<GeTuiMessageEvent> {
    @Override
    public void onApplicationEvent(GeTuiMessageEvent event) {
        GeTuiBo geTuiBo = (GeTuiBo) event.getSource();
        //填写应用配置
        GtApiConfiguration apiConfiguration = new GtApiConfiguration();
        apiConfiguration.setAppId("GJOmMrKCojAR5FA3B2GUT2");
        apiConfiguration.setAppKey("4l7tEYkASn68I7xH4Qtno1");
        apiConfiguration.setMasterSecret("hdMVWTPlyP92aVISP4lTA3");
        // 接口调用前缀，请查看文档: 接口调用规范 -> 接口前缀
        apiConfiguration.setDomain("https://restapi.getui.com/v2/");
        // 实例化ApiHelper对象，用于创建接口对象
        ApiHelper apiHelper = ApiHelper.build(apiConfiguration);

        //1.创建消息推送API
        PushApi pushApi = apiHelper.creatApi(PushApi.class);

        //根据cid进行单推
        PushDTO<Audience> pushDTO = new PushDTO<Audience>();
        // 设置推送参数
        pushDTO.setRequestId(System.currentTimeMillis() + "");

        /**** 设置个推通道参数 *****/
        PushMessage pushMessage = new PushMessage();
        pushMessage.setTransmission(JSON.toJSONString(geTuiBo));
        pushDTO.setPushMessage(pushMessage);

        /*设置接收人信息*/
        Audience audience = new Audience();
        pushDTO.setAudience(audience);
        //audience.addCid("05e74c38fd2f352b2d9d271e6a12a88d");
        audience.setAlias(List.of(geTuiBo.getCustomerId().toString()));

        // 进行cid单推
        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByAlias(pushDTO);
        System.out.println(apiResult);
    }
}
